package com.leapp.sk.ui.fragment
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.bean.UserManager
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.util.KEY_NICKNAME
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.fragment.BaseFragmentMVP
import com.leapp.sk.R
import com.leapp.sk.app.PERSONAL_MINE
import com.leapp.sk.bean.request.personal.MineRequestEntity
import com.leapp.sk.bean.response.personal.PersonalMineResponseEntity
import com.leapp.sk.binder.MyDataBinder
import com.leapp.sk.sever.personal.MineSever
import com.leapp.sk.ui.activity.personal.*
import com.example.util.finallist.KEY_AVATAR
import com.example.util.finallist.KEY_SEX
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.viewdelegate.MyDelegate
/**
 * 我的页面
 * @author  chaohaipeng  on 2018/7/4.
 */
class MyFragment : BaseFragmentMVP<MyDelegate>() {

    override fun initView(p0: Bundle?) {
    }
    override fun initData() {

        viewDelegate.initData()
        httpMine()
    }
    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, PersonalDataActivity::class.java)
            activity.startActivity(intent)
        },R.id.ima_fm_avatar)
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, ContactTecherActivity::class.java)
            activity.startActivity(intent)
        },R.id.rl_fm_contact_techer)

        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, ContactCustomerServiceActivity::class.java)
            activity.startActivity(intent)
        },R.id.rl_fm_custom_service)

        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, SettingActivity::class.java)
            activity.startActivity(intent)
        },R.id.rl_fm_setting)
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, FeedbackActivity::class.java)
            activity.startActivity(intent)
        },R.id.rl_fm_personal_feedback)
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(activity, ContanctActivity::class.java)
            activity.startActivity(intent)
        },R.id.rl_fm_class_index)
    }

    override fun onResume() {
        super.onResume()
        val string = LKSPUtil.getInstance().getString(KEY_AVATAR)
        val nick = LKSPUtil.getInstance().getString(KEY_NICKNAME)
        val sex = LKSPUtil.getInstance().getInt(KEY_SEX)
        viewDelegate?.setHeadData(string)
        viewDelegate?.setSex(sex)
        val accountTable = AccountDao.getInstance().queryById(UserManager.userId)
        if (accountTable==null||!TextUtils.equals(accountTable?.avatar,string)||!TextUtils.equals(accountTable?.nick,nick)){
            accountTable?.avatar=string
            accountTable?.nick=nick
            AccountDao.getInstance().createOrUpdate(accountTable)
        }
    }


    override fun getDelegateClass(): Class<MyDelegate> {
        return MyDelegate::class.java
    }
    override fun getDataBinder(): BaseDataBinder<*, *> {
       return MyDataBinder()
    }
    /**
     * 获取个人信息
     */
    private fun httpMine() {
        val baseRequestEntity = MineRequestEntity()
        baseRequestEntity.id=baseRequestEntity.userId
        LKHttp.post(PERSONAL_MINE, baseRequestEntity, PersonalMineResponseEntity::class.java, object : BaseCallBack<PersonalMineResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: PersonalMineResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    MineSever.getInstance().binderData(activity,viewDelegate,obj)
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }




}